cmake_minimum_required (VERSION 3.26)
project(WuhuaEngine VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 20)



# #set  output dir
# if(NOT DEFINED CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG)
#   set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG 	${CMAKE_CURRENT_SOURCE_DIR}/Build/Debug)
# endif()
# if(NOT DEFINED CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE)
#   set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE 	${CMAKE_CURRENT_SOURCE_DIR}/Build/Release)
# endif()


set(EngineMiscScriptsDirectory ${CMAKE_CURRENT_SOURCE_DIR}/../../Tools/MiscScripts)
set(EngineSourceDirectory ${CMAKE_CURRENT_SOURCE_DIR}/Source)

#set 3rdparty path
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/rtaudio)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/Vulkan)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/cgltf)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/Precompiled)


file(GLOB_RECURSE AllCppFiles Source/*.cpp Source/*.h )

set(EngineCoreCompileFiles  ${AllCppFiles} )
set(EngineCoreLinks   
Vulkan  
cgltf
rtaudio
EnginePrecompiledLibs
)

if(LINUX)
  list(APPEND EngineCoreLinks X11 Xrandr)

endif()


if(NOT WIN32)
  list(APPEND EngineCoreLinks -luuid)  # for uuid
endif()

if(WIN32)
  # for windows api imm.h ,  influence ime input
  list(APPEND EngineCoreLinks imm32)
  list(APPEND EngineCoreLinks Ws2_32) # for network
  list(APPEND EngineCoreLinks Shell32) # for find download path 
  list(APPEND EngineCoreLinks Dwmapi) # for dwmapi.h
  
  # add .natvis file to EngineCoreCompileFiles
  # .nativs file is help to make a custom view of debugging variable,
  # both valid in Visual Stidio and Visual Stidio Code on Windows.
  # if you want to use it in Visual Stidio Code with cmake, you need Add :
  #  "cmake.debugConfig": {
  #  "visualizerFile": "D:/GitHub/WuhuaEngine/Engine/WuhuaEngine.natvis",
  #  "showDisplayString": true
  #  }
  # in setting.json
  # see how to use .natvis in Visual Stidio Code without cmake https://code.visualstudio.com/docs/cpp/launch-json-reference
  # see how to write .nativs file: https://docs.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects?view=vs-2019

  list(APPEND EngineCoreCompileFiles "${CMAKE_CURRENT_LIST_DIR}/WuhuaEngine.natvis")

endif()

set(EngineCoreInclude  ${CMAKE_CURRENT_LIST_DIR}/Source/)

add_library(EngineCore SHARED)
add_dependencies(EngineCore CopyPrecompiledLibDlls)

target_sources(EngineCore PRIVATE ${EngineCoreCompileFiles})


target_link_libraries(EngineCore PUBLIC ${EngineCoreLinks})
target_include_directories(EngineCore PUBLIC ${EngineCoreInclude} )

#set DEBUG Macro
target_compile_definitions(EngineCore PRIVATE "$<$<CONFIG:DEBUG>:DEBUG>")
#set Export Macro
if(WIN32)
  target_compile_definitions(EngineCore PRIVATE "BUILD_WHENGINE_DLL")
ENDIF ()

#set system Macro
if(WIN32)
  target_compile_definitions(EngineCore PRIVATE "WIN32")
ENDIF ()

if(LINUX)
  target_compile_definitions(EngineCore PRIVATE "LINUX")
else() 
endif ()

if(MACOS)
  target_compile_definitions(EngineCore PRIVATE "MACOS")
ENDIF ()

if(IOS)
  target_compile_definitions(EngineCore PRIVATE "IOS")
ENDIF ()
if(ANDROID)
  target_compile_definitions(EngineCore PRIVATE "ANDROID")
ENDIF ()

#add /P to generate preprocessed file
#if(MSVC)
  #target_compile_options(EngineCore PRIVATE "/P")
#endif()


add_custom_target(PrepareContent
USES_TERMINAL 

#compile shaders 
COMMAND  $<TARGET_FILE:ShaderCompilerTool> 
  ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/  
  ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE}

  COMMAND  $<TARGET_FILE:ShaderCompilerTool>
  ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/AntiAlising
  ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE}

  COMMAND  $<TARGET_FILE:ShaderCompilerTool>
  ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/Axis
  ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE}

  COMMAND  $<TARGET_FILE:ShaderCompilerTool> 
  ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/CanvasWidget
  ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE}

  
  COMMAND  $<TARGET_FILE:ShaderCompilerTool>
  ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/ImageWidget  
  ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE}

  COMMAND  $<TARGET_FILE:ShaderCompilerTool>
  ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/Circle  
  ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE}

  COMMAND  $<TARGET_FILE:ShaderCompilerTool>
  ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/Lights
  ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE}

  COMMAND  $<TARGET_FILE:ShaderCompilerTool> 
    ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/RectWidget  
    ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE}

  COMMAND  $<TARGET_FILE:ShaderCompilerTool>
  ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/SkyBox  
  ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE}

  COMMAND  $<TARGET_FILE:ShaderCompilerTool>
  ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/PixelObject  
  ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE}

#convert all spv file to text file
COMMAND  $<TARGET_FILE:BinaryToText> ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/ .spv
COMMAND  $<TARGET_FILE:BinaryToText> ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/AntiAlising .spv
COMMAND  $<TARGET_FILE:BinaryToText> ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/Axis .spv
COMMAND  $<TARGET_FILE:BinaryToText> ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/CanvasWidget .spv
COMMAND  $<TARGET_FILE:BinaryToText> ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/ImageWidget .spv
COMMAND  $<TARGET_FILE:BinaryToText> ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/Circle .spv
COMMAND  $<TARGET_FILE:BinaryToText> ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/Lights .spv
COMMAND  $<TARGET_FILE:BinaryToText> ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/RectWidget .spv
COMMAND  $<TARGET_FILE:BinaryToText> ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/SkyBox .spv
COMMAND  $<TARGET_FILE:BinaryToText> ${CMAKE_CURRENT_LIST_DIR}/Source/Shaders/PixelObject .spv

#convert all icon to text file
COMMAND  $<TARGET_FILE:BinaryToText> ${CMAKE_CURRENT_LIST_DIR}/Source/Icons/ .png
COMMAND  $<TARGET_FILE:BinaryToText> ${CMAKE_CURRENT_LIST_DIR}/Source/Icons/Skybox .png
)

add_dependencies(PrepareContent  BinaryToText ShaderCompilerTool CopyDirectoryTool)
add_dependencies(EngineCore  PrepareContent)

#generate reflect files
add_custom_target(Reflected_EngineCore 
    USES_TERMINAL 
    COMMAND  $<TARGET_FILE:EngineReflectionTool>  ${EngineSourceDirectory} "EngineCore"
)
add_dependencies(Reflected_EngineCore EngineReflectionTool)

add_dependencies(EngineCore  Reflected_EngineCore)


